package cn.mumzone.pos.presenter;

import java.util.List;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.event.MemberChangeEvent;
import cn.mumzone.pos.event.RemoveServiceEvent;
import cn.mumzone.pos.model.GoodsEntity;
import cn.mumzone.pos.model.ReturnEntity;
import cn.mumzone.pos.model.ServerEntity;
import cn.mumzone.pos.model.ServerResultEntity;
import cn.mumzone.pos.model.ServiceEntity;
import cn.mumzone.pos.model.ShopUsableInfoEntity;
import cn.mumzone.pos.model.source.ReserveRepository;
import cn.mumzone.pos.presenter.contract.ServiceListContract;
import rx.Subscription;
import rx.functions.Action1;

/**
 * Created by qiuzhenhuan on 2019/6/26.
 */

public class ServiceListPresenter extends AbsPresenter<ServiceListContract.View, ReserveRepository> implements ServiceListContract.Presenter {

    @Override
    public void loadServiceList(String memberId) {
        ApiCallBack<ShopUsableInfoEntity> callBack = new ApiCallBack<ShopUsableInfoEntity>() {
            @Override
            public void onSuccess(ShopUsableInfoEntity usableInfoEntity) {
                mView.showServiceList(usableInfoEntity.getServices());
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }

            @Override
            public void onNetworkError(BaseResponse response) {
                mView.showNetError();
            }
        };
        mRepo.getShopUsableServiceList(memberId, callBack);
        addSubscrebe(callBack);
    }

    @Override
    public void loadServerList(String updateType, String appointmentId, String appointmentTime, List<ServiceEntity> serviceList) {


        ApiCallBack<ServerResultEntity> callBack = new ApiCallBack<ServerResultEntity>() {
            @Override
            public void onSuccess(ServerResultEntity serverResult) {
                mView.showServerList(serverResult);
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };

        mRepo.getUsableTechnicianList(updateType, appointmentId, appointmentTime, serviceList, callBack);
        addSubscrebe(callBack);
    }

    @Override
    public void doAddServiceToReserve(ServiceEntity entity) {
        ApiCallBack<String> callBack = new ApiCallBack<String>() {
            @Override
            public void onSuccess(String id) {
                mView.showAddServiceToReserveSuccess(id);
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showAddServiceToReserveError();
                mView.showError(response.getMsg());

            }
        };
        mRepo.addServiceToReserve(entity, callBack);
        addSubscrebe(callBack);
    }

    @Override
    public boolean checkLocalAndRemotePrice(List<ServiceEntity> remoteList, List<ServiceEntity> localList) {
        boolean isUpdatePrice = false;
        if (ListUtil.isListEmpty(localList) || ListUtil.isListEmpty(remoteList)) {
            return false;
        }
        for (ServiceEntity localEntity : localList) {
            for (ServiceEntity remoteEntity : remoteList) {
                if (remoteEntity.getServiceId().equals(localEntity.getServiceId())) {
                    if (!StringUtils.isEmpty(localEntity.getOriginPrice()) && !StringUtils.isEmpty(remoteEntity.getOriginPrice())
                            && !localEntity.getOriginPrice().equals(remoteEntity.getOriginPrice())) {
                        localEntity.setOriginPrice(remoteEntity.getOriginPrice());
                        isUpdatePrice = true;
                    }
                    if (!StringUtils.isEmpty(localEntity.getMemberPrice()) && !StringUtils.isEmpty(remoteEntity.getMemberPrice())
                            && !localEntity.getMemberPrice().equals(remoteEntity.getMemberPrice())) {
                        localEntity.setMemberPrice(remoteEntity.getMemberPrice());
                        isUpdatePrice = true;
                    }
                }
            }
        }
        return isUpdatePrice;
    }

    public void initEvent() {
        Subscription memberChangeListener = RxBus.getDefault()
                .toObservable(MemberChangeEvent.class)
                .subscribe(new Action1<MemberChangeEvent>() {
                    @Override
                    public void call(MemberChangeEvent memberChangeEvent) {
                        String memberId = null == memberChangeEvent.getMemberEntity() ?
                                "" : StringUtils.get(memberChangeEvent.getMemberEntity().getMemberId());
                        loadServiceList(memberId);
                        mView.showMemberChange(memberChangeEvent.getMemberEntity());
                    }
                });
        addSubscrebe(memberChangeListener);

        Subscription removeServiceListener = RxBus.getDefault()
                .toObservable(RemoveServiceEvent.class)
                .subscribe(new Action1<RemoveServiceEvent>() {
                    @Override
                    public void call(RemoveServiceEvent removeServiceEvent) {
                        mView.showRemoveService(removeServiceEvent.getServiceEntity());
                    }
                });
        addSubscrebe(removeServiceListener);


    }
}
